package com.share.order.listener;

import com.rabbitmq.client.Channel;
import com.share.common.rabbit.constant.MqConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

//RabbitMQ 测试 消费者
@Slf4j
@Component
public class TestListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_TEST,durable = "true"),  //监听的队列
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST,durable = "true"),  //交换机名
            key = MqConst.ROUTING_TEST  //交换机与队列绑定的key
    ))
    public void testMQ(String message){
        log.info("接收消息：{}", message);
    }

    //手动处理消息发送成功返回的确认消息
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST,durable = "true"), //交换机
            value = @Queue(value = MqConst.QUEUE_CONFIRM,durable = "true"), //专用于确认消息的队列
            key = MqConst.ROUTING_CONFIRM //binding key
    ))
    public void confirm(String content, Message message, Channel channel) throws IOException {
        log.info("接收确认消息：{}", content);

        // false 确认一个消息，true 批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }
}
